<?php

class Install
{

  public static function createTables()
  {
    uses('kernel/adb');

    /*
    CREATE TABLE `acl_groups` (
      `group_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
      `group_name` varchar(255) NOT NULL,
      PRIMARY KEY (`group_id`),
      UNIQUE KEY `group_name` (`group_name`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    */

    $table = Adb_Table::getInstance('test');
    $table->setOption(Adb_Table::OPTION_INNODB);
    $table->create();

    $field = $table->getField('group_name');
    $field->setType('text');
    $field->setLength(255);
    $field->create();

    $table->createConstraint('group_name', Adb_Table::CONSTRAINT_UNIQUE, 'group_name');

    DB::getInstance()->query('ALTER TABLE `acl_groups_permissions` ADD CONSTRAINT `acl_groups_permissions_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `acl_groups` (`group_id`) ON DELETE CASCADE');
  }

  public static function createUser($email, $password)
  {
    $data = array(
      'user_email' => $email,
      'user_password' => self::hash($password)
    );

    $user_id = DB::getInstance()->query()->from('users')->select('user_id')->where('user_email = :user_email')->execute($data)->fetchOne();

    if (! empty($user_id)) {
      throw new LoginException('User already exists');
    }

    DB::getInstance()->insert('users', $data)->execute($data);

    $user_id = DB::getInstance()->lastInsertId();

    return $user_id;
  }

  public static function saveGroup($name)
  {
    $data = array(
      'group_name' => $name
    );

    $group_id = DB::getInstance()->query()->from('acl_groups')->select('group_id')->where('group_name = :group_name')->execute($data)->fetchOne();

    if (empty($group_id)) {
      DB::getInstance()->insert('acl_groups', $data)->execute($data);

      $group_id = DB::getInstance()->lastInsertId();
    }

    return $group_id;
  }

  public static function savePermission($name, $description = '')
  {
    $data = array(
      'permission_name' => $name,
      'permission_description' => $description
    );

    $permission_id = DB::getInstance()->query()->from('acl_groups')->select('permission_id')->where('group_name = :group_name')->execute($data)->fetchOne();

    if (empty($permission_id)) {
      DB::getInstance()->insert('acl_permissions', $data)->execute($data);

      $permission_id = DB::getInstance()->lastInsertId();
    }

    return $permission_id;
  }

  public static function addUserGroup($user_id, $group_id)
  {
    $data = array(
      'group_id' => $group_id,
      'user_id' => $user_id
    );

    DB::getInstance()->query('INSERT IGNORE INTO acl_groups_users VALUES (:group_id, :user_id)')->execute($data);
  }

  public static function addUserPermission($user_id, $permission_id)
  {
    $data = array(
      'permission_id' => $permission_id,
      'user_id' => $user_id
    );

    DB::getInstance()->query('INSERT IGNORE INTO acl_permissions_users VALUES (:permission_id, :user_id)')->execute($data);
  }

  public static function addGroupPermission($group_id, $permission_id)
  {
    $data = array(
      'permission_id' => $permission_id,
      'group_id' => $group_id
    );

    DB::getInstance()->query('INSERT IGNORE INTO acl_groups_permissions VALUES (:permission_id, :group_id)')->execute($data);
  }

}
